package com.unitd.frame.comm.utils;

import java.security.SecureRandom;
import java.util.UUID;

/**
 * @desc 封装各种生成唯一性ID算法的工具类.
 */
public class IdGenUtils {

	private static SecureRandom random = new SecureRandom();

	/**
	 * @desc 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
	 * @return
	 */
	public static String uuid() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	/**
	 * @desc 使用SecureRandom随机生成Long.
	 */
	public static Long randomLong() {
		return Math.abs(random.nextLong());
	}

	/**
	 * @desc 使用SecureRandom随机生成Long字符串.
	 */
	public static String randomLongStr() {
		return String.valueOf(Math.abs(random.nextLong()));
	}

	/**
	 * @desc 基于Base62编码的SecureRandom随机生成bytes.
	 * @param length 生成的字符串长度
	 * @return
	 */
	public static String randomBase62(int length) {
		byte[] randomBytes = new byte[length];
		random.nextBytes(randomBytes);
		return EncodeUtils.encodeBase62(randomBytes);
	}

	public static void main(String[] args) {
		System.out.println(IdGenUtils.uuid());
		System.out.println(IdGenUtils.uuid().length());
		for (int i = 0; i < 1000; i++) {
			System.out.println(IdGenUtils.randomLong() + "  " + IdGenUtils.randomBase62(5));
		}
	}
}